package cn.mhome.merchant.util;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 时间工具类
 * 
 * @author 吕鹏
 */
public class DateUtil {

	public static final String DEFAULT_PATTERN = "yyyy-MM-dd HH:mm:ss";

	/**
	 * 获取操作时间 lvpeng
	 * 
	 * @return
	 */
	public static Date getOperateTime() {
		return (new DateUtilInner()).getOperateTime();
	}

	/**
	 * 项目周期
	 * 
	 * @param time1
	 * @param time2
	 * @return
	 */
	public static String getProjectCycle(Date time1, Date time2) {
		return (new DateUtilInner()).getProjectCycle(time1, time2);
	}

	/**
	 * 获取相隔interval天的时间 并返回pattern类型的string
	 * 
	 * @param interval
	 * @param starttime
	 * @param pattern
	 * @return
	 */
	public static String getDate(String interval, Date starttime, String pattern) {
		return (new DateUtilInner()).getDate(interval, starttime, pattern);
	}

	/**
	 * 格式化日期
	 */
	public static String getNowByFormat(String format) {
		return (new DateUtilInner()).getNowByFormat(format);
	}

	/**
	 * 将字符串类型转换为时间类型 如果传递过来的字符串为 yyyy-mm-dd hh:mm 则自动补全 :ss
	 * 
	 * @param str
	 * @return
	 */
	public static Date str2Date(String str) {
		return (new DateUtilInner()).str2Date(str);
	}

	/**
	 * 将字符串按照pattern类型转换为时间类型
	 * 
	 * @param str
	 * @param pattern
	 * @return
	 */
	public static Date str2Date(String str, String pattern) {
		return (new DateUtilInner()).str2Date(str, pattern);
	}

	/**
	 * 将时间格式化
	 * 
	 * @return
	 */
	public static Date datePattern(Date date) {
		return (new DateUtilInner()).datePattern(date);
	}

	/**
	 * 将long转成时间格式
	 * 
	 * @param datelong
	 * @param format
	 * @return
	 */
	public static String long2date(long datelong, String format) {
		return (new DateUtilInner()).long2date(datelong, format);
	}

	/**
	 * 将时间格式化
	 */
	public static Date datePattern(Date date, String pattern) {
		return (new DateUtilInner()).datePattern(date);
	}

	/**
	 * 将date转换成默认时间格式的字符串
	 * 
	 * @param date
	 * @return
	 */
	public static String date2Str(Date date) {
		return (new DateUtilInner()).date2Str(date);
	}

	/**
	 * 将date转换成format格式的字符串
	 * 
	 * @param date
	 * @return
	 */
	public static String date2Str(Date date, String format) {
		return (new DateUtilInner()).date2Str(date, format);
	}

	/**
	 * 获取昨天
	 * 
	 * @param date
	 * @return
	 * @throws Exception
	 */
	public static Date getLastDate(Date date) {
		return (new DateUtilInner()).getLastDate(date);
	}

	/**
	 * 获取昨天
	 * 
	 * @param date
	 * @param pattern
	 * @return
	 * @throws Exception
	 */
	public static String getLastDate(Date date, String pattern) {
		return (new DateUtilInner()).getLastDate(date, pattern);
	}
	
	/**
	 * 获取前天
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static String getTheDayBeforeYesterday(Date date, String pattern){
		return (new DateUtilInner()).getTheDayBeforeYesterday(date, pattern);
	}

	/**
	 * 获取上周第一天（周一）
	 * 
	 * @param date
	 * @return
	 * @throws Exception
	 */
	public static Date getLastWeekStart(Date date) {
		return (new DateUtilInner()).getLastWeekStart(date);
	}

	/**
	 * 返回指定月的最后一天 比如 201002 获取2月的最后一天
	 * 
	 * @param year
	 * @param month
	 * @return
	 */
	public static int getLastDayOfMonth(int year, int month) {
		return (new DateUtilInner()).getLastDayOfMonth(year, month);
	}

	/**
	 * 获取上周最后一天（周末）
	 * 
	 * @param date
	 * @return
	 * @throws Exception
	 */
	public static Date getLastWeekEnd(Date date) {
		return (new DateUtilInner()).getLastWeekEnd(date);
	}

	/**
	 * 改更现在时间
	 */
	public static Date changeDate(String type, int value) {
		return (new DateUtilInner()).changeDate(type, value);
	}

	/**
	 * 更改时间
	 */
	public static Date changeDate(Date date, String type, int value) {
		return (new DateUtilInner()).changeDate(type, value);
	}

	/**
	 * 比较时间是否在这两个时间点之间
	 * 
	 * @param time1
	 * @param time2
	 * @return
	 */
	public static boolean checkTime(String time1, String time2) {
		return (new DateUtilInner()).checkTime(time1, time2);
	}

	/**
	 * 检查输入日期是否是interval月之内的日期
	 * 
	 * @param time
	 * @param interval
	 * @return
	 */
	public static boolean checkOnly6Month(String time, int interval) {
		return (new DateUtilInner()).checkOnly6Month(time, interval);
	}

	/**
	 * 计算两个日期相隔的天数
	 * 
	 * @param firstString
	 * @param secondString
	 * @return
	 */
	public static int nDaysBetweenTwoDate(String starttime, String endtime) {
		return (new DateUtilInner()).nDaysBetweenTwoDate(starttime, endtime);
	}

	/**
	 * 计算两个日期相隔的天数
	 * 
	 * @param firstString
	 * @param secondString
	 * @return
	 */
	public static int nDaysBetweenTwoDate(Date startTime, Date endTime) {
		return (new DateUtilInner()).nDaysBetweenTwoDate(startTime, endTime);
	}


	/**
	 * 获取当月第一天和下月第一天
	 * 
	 * @return
	 */
	public static List<Date> getFirstAndLastDayOfMonth() {
		return (new DateUtilInner()).getFirstAndLastDayOfMonth();
	}

	/**
	 * 获取今天和昨天开始时间
	 * 
	 * @return
	 */
	public static List<Date> getLastDayAndToday() {
		return (new DateUtilInner()).getLastDayAndToday();
	}

	/**
	 * 获取今天和明天开始时间
	 * 
	 * @return
	 */
	public static List<Date> getCurDayAndTomorrowDay() {
		return (new DateUtilInner()).getCurDayAndTomorrowDay();
	}

	/**
	 * 获取当前时间小时和下个小时
	 * 
	 * @return
	 */
	public static List<Date> getCurHourAndNextHour() {
		return (new DateUtilInner()).getCurHourAndNextHour();
	}

	/**
	 * 获取当前时间距离结束时间毫秒数
	 * 
	 * @param toDate
	 * @return
	 */
	public static long getNowInteval(Date toDate) {
		return (new DateUtilInner()).getNowInteval(toDate);
	}

	/**
	 * 比较当前时间是否比原时间最多相差一分钟
	 * 
	 * @param d
	 *            原时间
	 * @return
	 */
	public static boolean checkOneMinute(Date d) {
		return (new DateUtilInner()).checkOneMinute(d);
	}

	/**
	 * 比较当前时间和原时间是否在同一天
	 * 
	 * @param d
	 * @return
	 */
	public static boolean isTheSameDay(Date d) {
		return (new DateUtilInner()).isTheSameDay(d);
	}

	/**
	 * 判断当前时候是否是忙时 是返回true 反之 false ||请求时间与2取摸 整出的返回true 反之false
	 * 
	 * @return
	 */
	public static boolean checkHotTime() {
		return (new DateUtilInner()).checkHotTime();
	}

	public static long getUTCFromDateStr(String time, String format) {
		return (new DateUtilInner()).getUTCFromDateStr(time, format);
	}


	/**
	 * 判断当前日期是星期几
	 * 
	 * 修要判断的时间
	 * 
	 * @return dayForWeek 判断结果
	 * @Exception 发生异常
	 */
	public static int getDayForWeek(Date date) throws Exception {
		return (new DateUtilInner()).getDayForWeek(date);
	}

	public static String[] getWeekDate(Date date) {
		return (new DateUtilInner()).getWeekDate(date);
	}

	/**
	 * 当前时间+(正负数)
	 * 
	 * @param date
	 * @param days
	 * @return
	 */
	public static Date modifyDate(Date date, int hour, int min) {
		return (new DateUtilInner()).modifyDate(date, hour, min);
	}

	/**
	 * 当前时间+(正负数)
	 * 
	 * @param date
	 * @param days
	 * @return
	 */
	public static Date modifyDate(Date date, int day) {
		return (new DateUtilInner()).modifyDate(date, day);
	}

	/**
	 * 
	 */

	/**
	 * 获取操作时间
	 * 
	 * @return
	 */
	public static String formatDate(Date date) {
		return (new DateUtilInner()).formatDate(date);
	}



	/**
	 * 通过两个日期 得到两个日期的周期
	 * 
	 * @return 2014-05-07~2014-05-09
	 */
	public static String getSaleCycle(Date time1, Date time2) {
		return (new DateUtilInner()).getSaleCycle(time1, time2);
	}


	/**
	 * 第一个时间小于第二个时间 返回true
	 */
	public static boolean compareTime(Date oneTime, Date twoTime) {
		return (new DateUtilInner()).compareTime(oneTime, twoTime);
	}

	/**
	 * 再一组Long集合中找到最小值和最大值
	 * 
	 * @return
	 */
	public static List<Long> findMinAndMaxValue(List<Long> values) {
		return (new DateUtilInner()).findMinAndMaxValue(values);
	}

	/**
	 * 获取24小时以后的时间
	 * 
	 * @param today
	 * @return
	 */
	public static Date getTomorrowByToday(Date today) {
		return (new DateUtilInner()).getTomorrowByToday(today);
	}
	
	public static String getNextDay(Date lastDate, int day, String dateFormat) {
		return (new DateUtilInner()).getNextDay(lastDate, day , dateFormat);
	}
	

	/**
	 * 根据今天获取本日，本周，本月的开始时间
	 * 
	 * @param today
	 * @return
	 */
	public static String[] getThreeDateByToday(Date today) {
		return (new DateUtilInner()).getThreeDateByToday(today);
	}

	/***
	 * TimeStamp转换成字符串
	 * 
	 * @param time
	 * @param dateFormat
	 * @return
	 */
	public static String getTimeStampString(Timestamp time, SimpleDateFormat dateFormatParam) {
		return (new DateUtilInner()).getTimeStampString(time, dateFormatParam);
	}

	public static Date changeDateByDay(Date date, int day) {
		return (new DateUtilInner()).changeDateByDay(date, day);
	}

	/**
	 * 获取两天时间差
	 * 
	 * @param smdate
	 * @param bdate
	 * @return
	 */
	public static int daysBetween(Date smdate, Date bdate) {
		return (new DateUtilInner()).daysBetween(smdate, bdate);
	}


	public static void main(String[] args) {
		Date date = str2Date("2016-08-20 18:00:00");
		int day = nDaysBetweenTwoDate(new Date(), date);

		System.out.println(day);
	}

}
